#ifndef _DATA_SVR_CLIENT_H_
#define _DATA_SVR_CLIENT_H_

#include <vector>
#include <map>
#include <log4cxx/logger.h>

#include "SocketConnPool.h"
#include "CnfSvrClient.h"

#define DS_WRITE_MODE_NORMAL 0
#define DS_WRITE_MODE_APPEND 1
#define DS_CRC_CHUNK_SIZE (1024 * 1024)


using namespace log4cxx;
using std::vector;
using std::map;

struct DS_FILE_CONTROL_INFO {
	vector<uint32_t> crcs;
};

class DataSvrClient {
public:
        int init(const char * svr_ip, const unsigned svr_port, const int conn_pool_size, const int conn_timeout, \
                                const int send_timeout, const int recv_timeout, const int max_response_size, const int data_svr_id);

        int create_file(int32_t dir_id, int64_t file_id);
        int delete_file(int32_t dir_id, int64_t file_id);
        int read_file(int32_t dir_id, int64_t file_id, int64_t begin_pos, int32_t len, char * buf);
	int write_file(int32_t dir_id, int64_t file_id, int64_t begin_pos, int32_t len, const char * buf, int32_t mode);
        int get_file_control_info(int32_t dir_id, int64_t file_id, DS_FILE_CONTROL_INFO & info, int64_t & file_size);
	int replicate(int32_t dir_id, int64_t file_id, int32_t src_ds_id, int64_t src_file_size, vector<uint32_t> src_crcs);
	int truncate(int32_t dir_id, int64_t file_id, int64_t size);
private:
        string _svr_ip;
        unsigned short _svr_port;
        int _conn_pool_size;
        int _conn_timeout;
        int _send_timeout;
        int _recv_timeout;
        int _max_response_size;
	int _data_svr_id;
        SocketConnPool _pool;
        static LoggerPtr _logger;
};

class DataSvrClientGroup {
public:
	int init(CnfSvrClient * csc);
	DataSvrClient * get_data_svr_client(int ds_id);
private:
	pthread_mutex_t _mutex;
	map<int, DataSvrClient *> _clients;
	CnfSvrClient * _csc;
	static LoggerPtr _logger;
};

#endif
